const arr = [2, 3, 4, 5, 6]

Array.prototype.myUnshift = function (...arr) {
  if (!arr) return
  // const num = []
  for (let j = this.length - 1; j >= 0; j--) {
    this[j + arr.length] = this[j]
  }
  for (let i = 0; i < arr.length; i++) {
    this[i] = arr[i]
  }
  return this.length
}

// let c = arr.myUnshift(1, 2, 3, 4)
// console.log(c, arr)

Array.prototype.myShift = function () {
  const num = this[0]
  for (let i = 0; i < this.length; i++) {
    this[i] = this[i + 1]
  }
  this.length = this.length - 1
  return num
}

// let a = arr.myShift()
// console.log(a, arr)

Array.prototype.myPush = function (...arr) {
  if (!arr) return
  for (let i = 0; i < arr.length; i++) {
    this[this.length] = arr[i]
  }
  return this.length
}

// let b = arr.myPush()
// console.log(b, arr)

Array.prototype.myPop = function () {
  const num = this[this.length - 1]
  this.length = this.length - 1
  return num
}

// let d = arr.myPop()
// console.log(d, arr)

Array.prototype.mySort = function (fn) {
  if (fn(this[0], this[1]) > 0) {
    for (let j = 0; j < this.length; j++) {
      for (let i = 0; i < this.length; i++) {
        if (this[i] > this[i + 1]) {
          let a = this[i]
          this[i] = this[i + 1]
          this[i + 1] = a
        }
      }
    }
  } else if (fn(this[0], this[1]) < 0) {
    for (let j = 0; j < this.length; j++) {
      for (let i = 0; i < this.length; i++) {
        if (this[i] > this[i + 1]) {
          let a = this[i + 1]
          this[i + 1] = this[i]
          this[i] = a
        }
      }
    }
  }
}

const ars = [4, 2, 5, 1, 3]

// let i = ars.sort((a, b) => a - b)
let i = ars.mySort((a, b) => b - a)

console.log(ars)
// console.log(i)
